package com.sun.umbrella.service.bns.res.impl;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.sun.core.enums.BaseBizEnum;
import com.sun.core.enums.BaseErrorEnum;
import com.sun.core.enums.DeletedEnum;
import com.sun.core.enums.EnumUtil;
import com.sun.core.ex.BusinessCheckFailException;
import com.sun.core.ex.BusinessProcessFailException;
import com.sun.star.base.util.BeanUtil;
import com.sun.star.base.util.CollectionUtil;
import com.sun.star.base.util.StringUtil;
import com.sun.umbrella.dao.ResInfoMapper;
import com.sun.umbrella.dao.RoleResRelationMapper;
import com.sun.umbrella.dao.SysDefMapper;
import com.sun.umbrella.domain.bo.ResInfoBO;
import com.sun.umbrella.domain.bo.SysBO;
import com.sun.umbrella.domain.entity.ResInfoDO;
import com.sun.umbrella.domain.enums.ResTypeEnum;
import com.sun.umbrella.domain.vo.ResSaveVO;
import com.sun.umbrella.domain.vo.ResUpdateVO;
import com.sun.umbrella.manager.res.ResInfoManager;
import com.sun.umbrella.service.bns.res.ResInfoService;
import com.sun.umbrella.session.ContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @description 资源信息service服务类
 * @author: Jay
 * @date: 2018-04-09 16:30
 * @version: v1.0
 */
@Service
@Slf4j
public class ResInfoServiceImpl implements ResInfoService {

    @Autowired
    private ResInfoMapper resInfoMapper;

    @Autowired
    private ResInfoManager resInfoManager;

    @Autowired
    private ContextHolder contextHolder;

    @Autowired
    private SysDefMapper sysDefMapper;

    @Autowired
    private RoleResRelationMapper roleResRelationMapper;

    /**
     * 资源信息保存接口,相当于在给这颗树添加节点
     * <p>
     * 如果brotherId不为空，则说明新增的节点需要查到该兄弟节点的右边
     * 如果brotherId为空，则说明新增的节点直接添加在fatherId的下，如果该fatherId有孩子节点，则添加在最右边
     * </p>
     *
     * @param resSaveVO
     */
    @Override
    public Boolean saveRes(ResSaveVO resSaveVO) {
        // 参数校验
        this.checkResSaveVO(resSaveVO);
        // 取出兄弟id
        Integer brotherId = resSaveVO.getBrotherId();
        // 系统id
        Integer sysId = resSaveVO.getSysId();
        // 取出父节点id，如果
        Integer fatherId = resSaveVO.getFatherId() == 0 ? 1 : resSaveVO.getFatherId();
        // 如果兄弟id不为空，则将该节点插入到兄弟节点的右边，同时更新整棵树的受影响的节点的左右值
        if (brotherId != null) {
            ResInfoDO resInfoDO = this.fillDOInsertByBrother(resSaveVO, this.getResourceByIdAndJudge(brotherId, sysId));
            // 保存资源信息,将该节点插入到兄弟节点的右边
            return resInfoManager.saveRes(resInfoDO, resInfoDO.getResRgt() - 2);
        }
        // 如果兄弟id为空，则将该节点插入到父节点节点下，同时更新整棵树的受影响的节点的左右值
        ResInfoDO resInfoDO = this.fillDOInsertByFatherId(resSaveVO, this.getResourceByIdAndJudge(fatherId, sysId));
        return resInfoManager.saveRes(resInfoDO, resInfoDO.getResLft() - 1);
    }

    /**
     * 资源信息批量删除接口（逻辑删除）
     *
     * @param sysId
     * @param id
     */
    @Override
    public Boolean deleteRes(Integer sysId, Integer id) {
        if (sysId == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "sysId不能为空");
        }
        if (id == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源id不能为空");
        }
        // 如果有孩子节点、或者是根节点则无法删除
        ResInfoBO resInfoBO = resInfoMapper.getResourceById(id, sysId);
        if (resInfoBO == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源不存在");
        }
        // 根节点则无法删除
        if (resInfoBO.getFatherId() == null || resInfoBO.getFatherId().equals(BaseBizEnum.ZERO.getKey())) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "无法删除根节点");
        }
        // 如果有孩子节点则无法删除，必须要先删除子节点
        if (resInfoBO.getResRgt() == null || resInfoBO.getResLft() == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常");
        }
        if (resInfoBO.getResRgt() - resInfoBO.getResLft() > 1) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该目录有子目录，如果需要删除请先删除子目录");
        }
        // 删除节点 : 将大于将节点右值的节点的左右值都减去2 ，然后将is_deleted置为1
        return resInfoManager.deleteRes(sysId,resInfoBO.getResRgt(),id);
    }

    /**
     * 根据资源名称和系统类型查询资源集合
     *
     * @param resName
     * @param sysId
     * @return
     */
    @Override
    public List<ResInfoBO> queryRessourceList(String resName, Integer sysId) {
        if (sysId == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "sysId不能为空");
        }
        // 当资源名称不为空的时候，查询该资源节点的所有子节点和他本身; 如果它没有子孙节点，则返回这个节点本身
        if (!StringUtils.isEmpty(resName)) {
            return this.queryRessourceListByResNameAndSysId(resName, sysId);
        }
        // 当资源名称为空的时候,查询该系统下所有的资源
        return this.queryRessourceListBySysId(sysId);
    }

    /**
     * 根据资源id查询资源信息
     *
     * @param sysId
     * @param id
     * @return
     */
    @Override
    public ResInfoBO getResById(Integer id, Integer sysId) {
        ResInfoBO resInfoBO = resInfoMapper.getResourceById(id, sysId);
        if (resInfoBO != null) {
            resInfoBO.setResTypeStr(EnumUtil.getName(resInfoBO.getResType(), ResTypeEnum.class));
        }
        return resInfoBO;
    }

    /**
     * 资源信息更新（可能会移动节点）
     * <p>
     * <p>
     * 1.判断
     * 2.查询左移部分A、计算包间距m
     * 3.查询右移部分B、计算包间距n
     * 4.计算左移部分A的新左右值(v-n-1)
     * 6.计算右移部分B的新左右值(v+m+1)
     * 7.更新移动节点
     * </p>
     *
     * @param resUpdateVO
     * @return
     */
    @Override
    public Boolean updateResource(ResUpdateVO resUpdateVO) {
        // 参数校验
        this.checkResUpdateVO(resUpdateVO);
        // 取出传入的需要更新数据的资源id
        Integer id = resUpdateVO.getId();
        // 系统id
        Integer sysId = resUpdateVO.getSysId();
        // 判断该id对应的节点是否存在
        ResInfoBO resInfoBO = resInfoMapper.getResourceById(id, sysId);
        if (resInfoBO == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源不存在，无法进行更新操作");
        }
        // 取需要移动的节点的左值
        Integer nodeLft = resInfoBO.getResLft();
        // 取需要移动的节点的右值
        Integer nodeRgt = resInfoBO.getResRgt();
        if (nodeLft == null || nodeRgt == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
        }
        // 取到父节点id
        Integer newFatherId = resUpdateVO.getFatherId();
        // 取兄弟节点id
        Integer newBrotherId = resUpdateVO.getBrotherId();
        // 父节点为空则说明不移动节点
        if (newFatherId == null) {
            return this.updateNodeRes(null, resUpdateVO);
        }
        // 查询新的父节点信息
        ResInfoBO newFatherResBO = resInfoMapper.getResourceById(newFatherId, sysId);
        if (newFatherResBO == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源不存在，无法进行更新操作");
        }
        // 取新的父节点的左值
        Integer newFatherLft = newFatherResBO.getResLft();
        // 取新的父节点的右值
        Integer newFatherRgt = newFatherResBO.getResRgt();
        if (newFatherLft == null || newFatherRgt == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
        }
        Integer newBrotherLft = null;
        Integer newBrotherRgt = null;
        if (newBrotherId != null) {
            // 查询传入的兄弟节点信息
            ResInfoBO newBrotherResBO = resInfoMapper.getResourceById(newBrotherId, sysId);
            if (newBrotherResBO == null) {
                throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源不存在，无法进行更新操作");
            }
            // 取新的兄弟节点的左值
            newBrotherLft = newBrotherResBO.getResLft();
            // 取新的兄弟节点的右值
            newBrotherRgt = newBrotherResBO.getResRgt();
            if (newBrotherLft == null || newBrotherRgt == null) {
                throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
            }
        }
        // 取兄弟节点的左值为参照点。如果兄弟节点左值为空
        Integer index = (newBrotherLft == null ? newFatherLft : newBrotherLft);
        // 计算移动的节点需要改变的左右值
        return this.doMoveNode(nodeLft, index, newBrotherRgt, newFatherLft, sysId, nodeRgt, resUpdateVO);
    }


    /**
     * 计算移动的节点需要改变的左右值
     *
     * @param nodeLft       传入的节点的左值
     * @param index         计算偏移量
     * @param newBrotherRgt 传入的兄弟节点的右值
     * @param newFatherLft  传入的父节点的左值
     * @param sysId         系统id
     * @param nodeRgt       传入的节点的右值
     * @param resUpdateVO   传入的资源VO
     * @return
     */
    public Boolean doMoveNode(Integer nodeLft, Integer index,
                              Integer newBrotherRgt, Integer newFatherLft,
                              Integer sysId, Integer nodeRgt, ResUpdateVO resUpdateVO) {

        // 如果是左移 A:左移的部分  B:右移的部分
        if (nodeLft - index > 0) {
            // A左移节点的左阈值 : 兄弟节点不存在则父节点左值+1,兄弟节点存在则兄弟节点右值+1
            Integer lB = (newBrotherRgt == null ? (newFatherLft + 1) : (newBrotherRgt + 1));
            // A左移节点的右阈值 :传入节点的右值 + 1
            Integer rB = nodeLft - 1;
            // 计算A的包间距
            Integer moveLRgtNumber = nodeRgt - nodeLft;
            // 计算B的包间距
            Integer nodeMoveNumber = rB - lB;
            // 计算A左移的左右值
            List<ResInfoBO> otherNodeResInfoBOList = this.handlerNodeByRange(sysId, lB, rB, moveLRgtNumber + 1);
            // 计算B右移的左右值
            List<ResInfoBO> nodeResInfoBOList = this.handlerNodeByRange(sysId, nodeLft, nodeRgt, -nodeMoveNumber - 1);
            // 合并需要更新的资源信息
            nodeResInfoBOList.addAll(otherNodeResInfoBOList);
            // 更新节点信息
            return this.updateNodeRes(nodeResInfoBOList, resUpdateVO);
        }
        // 如果是右移 A:左移的部分  B:右移的部分
        if (nodeLft - index < 0) {
            // 计算B右移节点的左阈值
            Integer lA = nodeRgt + 1;
            // 计算B右移节点的右阈值
            Integer rA = (newBrotherRgt == null ? newFatherLft : newBrotherRgt);
            // 计算A的包间距
            Integer nodeMoveNumber = nodeRgt - nodeLft;
            // 计算B的包间距
            Integer otherNodeMoveNumber = rA - lA;
            // 计算B右移部分节点的左右值
            List<ResInfoBO> otherResInfoBOList = this.handlerNodeByRange(sysId, nodeLft, nodeRgt, otherNodeMoveNumber + 1);
            // 计算A左移部分节点的左右值
            List<ResInfoBO> nodeResInfoBOList = this.handlerNodeByRange(sysId, lA, rA, -nodeMoveNumber - 1);
            // 合并需要更新的资源信息
            nodeResInfoBOList.addAll(otherResInfoBOList);
            // 更新节点信息
            return this.updateNodeRes(nodeResInfoBOList, resUpdateVO);
        }
        throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "数据异常，操作失败");
    }


    /**
     * 根据sysId和id更新资源左右值
     *
     * @param resInfoBOList
     * @param resUpdateVO
     * @return
     */
    @Transactional(rollbackFor = BusinessProcessFailException.class)
    public Boolean updateNodeRes(List<ResInfoBO> resInfoBOList, ResUpdateVO resUpdateVO) {
        // 更新传入节点资源信息
        ResInfoDO resInfoDO = this.fillResInfoDOWithoutMoveNode(resUpdateVO);
        resInfoManager.updateResWithoutMoveNode(resInfoDO);
        // 如果不需要更新资源值，则直接return
        if (CollectionUtils.isEmpty(resInfoBOList)) {
            return Boolean.TRUE;
        }
        // 更新节点左右值
        for (ResInfoBO resInfoBO : resInfoBOList) {
            resInfoManager.updateResLftAndRgt(resInfoBO);
        }
        return Boolean.TRUE;
    }

    /**
     * 根据roleId和sysId查询资源基础信息
     *
     * @param roleId
     * @param sysId
     * @return 返回树的根节点
     */
    @Override
    public ResInfoBO queryRessourceListByRoleIdAndSysId(Integer roleId, Integer sysId) {
        if (roleId == null || sysId == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "sysId或roleId不能为空");
        }
        // 查询该roleId对应的资源id集合
        List<Integer> resIdList = roleResRelationMapper.selectResIdListByRoleIdAndSysId(sysId,roleId);
        if (CollectionUtils.isEmpty(resIdList)) {
            return null;
        }
        // 查询资源id集合对应的资源信息，返回一个根节点对象
        List<ResInfoDO> resInfoDOList = resInfoMapper.queryResourceInfoListByIdList(resIdList, sysId);
        if (CollectionUtils.isEmpty(resInfoDOList)) {
            return null;
        }
        return this.makeRessourceListToRootNode(BeanUtil.copyList(resInfoDOList, ResInfoBO.class));
    }

    /**
     * 查询项目资源
     *
     * @return
     */
    @Override
    public List<SysBO> querySysList() {
        return sysDefMapper.selectAllSys();
    }

    /*=================================================private method======================================================*/


    /**
     * 节点移动后的左右值计算
     *
     * @param sysId      系统id
     * @param lftRange   左阈值
     * @param rgtRange   右阈值
     * @param moveNumber 移动的相对步数
     * @return
     */
    private List<ResInfoBO> handlerNodeByRange(Integer sysId, Integer lftRange, Integer rgtRange, Integer moveNumber) {
        if (sysId == null || lftRange == null || rgtRange == null || moveNumber == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "参数传入不合法");
        }
        // 查询阈值内的节点
        List<ResInfoBO> resInfoBOList = resInfoMapper.selectNodeByRange(sysId, lftRange, rgtRange);
        if (CollectionUtils.isEmpty(resInfoBOList)) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
        }
        for (ResInfoBO nodeRes : resInfoBOList) {
            // 校验
            if (nodeRes == null) {
                throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
            }
            // 计算移动后的左右值
            Integer lft = nodeRes.getResLft();
            Integer rgt = nodeRes.getResRgt();
            if (lft == null || rgt == null) {
                throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "该id对应的资源数据异常，无法进行更新操作");
            }
            if (lft >= lftRange && lft <= rgtRange) {
                nodeRes.setResLft(lft + moveNumber);
            }
            if (rgt >= lftRange && rgt <= rgtRange) {
                nodeRes.setResRgt(rgt + moveNumber);
            }
        }
        return resInfoBOList;
    }


    /**
     * 更新数据的填充
     *
     * @param resUpdateVO
     * @return
     */
    private ResInfoDO fillResInfoDOWithoutMoveNode(ResUpdateVO resUpdateVO) {
        ResInfoDO resInfoDO = new ResInfoDO();
        resInfoDO.setGmtModified(new Date());
        resInfoDO.setFatherId(resUpdateVO.getFatherId());
        BeanUtil.copyProperties(resUpdateVO, resInfoDO);
        return resInfoDO;
    }

    /**
     * 填充参数，用于插入父节点下
     *
     * @param resInfoBO
     * @param resSaveVO
     * @return
     */
    private ResInfoDO fillDOInsertByFatherId(ResSaveVO resSaveVO, ResInfoBO resInfoBO) {
        if (resInfoBO.getResLft() == null || resInfoBO.getResRgt() == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "数据异常");
        }
        ResInfoDO resInfoDO = new ResInfoDO();
        BeanUtil.copyProperties(resSaveVO, resInfoDO);
        resInfoDO.setModifier(contextHolder.getSessionContext().getUserId());
        resInfoDO.setCreator(contextHolder.getSessionContext().getUserId());
        resInfoDO.setIsDeleted(DeletedEnum.NO.getKey());
        resInfoDO.setGmtCreate(new Date());
        resInfoDO.setGmtModified(new Date());
        resInfoBO.setSysId(resSaveVO.getSysId());
        // 设置左右值
        resInfoDO.setResLft(resInfoBO.getResRgt());
        resInfoDO.setResRgt(resInfoBO.getResRgt() + 1);
        // 设置父id
        resInfoDO.setFatherId(resInfoBO.getId());
        // 树深度 +1
        resInfoDO.setResLevel(resInfoBO.getResLevel() + 1);
        return resInfoDO;
    }

    /**
     * 根据资源id查询资源
     *
     * @param sysId
     * @param id
     * @return
     */
    private ResInfoBO getResourceByIdAndJudge(Integer id, Integer sysId) {
        if (id == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源Id不能为空");
        }
        if (sysId == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "sysId不能为空");
        }
        ResInfoBO resInfoBO = resInfoMapper.getResourceById(id, sysId);
        if (resInfoBO == null) {
            log.error(this.getClass().getName() + "#saveRes, 入参resSaveVO为 --> {}", id);
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "未查询到对应的资源");
        }
        return resInfoBO;
    }

    /**
     * 填充参数准备插入树、中
     *
     * @param resSaveVO
     * @return
     */
    private ResInfoDO fillDOInsertByBrother(ResSaveVO resSaveVO, ResInfoBO resInfoBO) {
        if (resInfoBO.getResLft() == null || resInfoBO.getResRgt() == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "数据异常");
        }
        ResInfoDO resInfoDO = new ResInfoDO();
        BeanUtil.copyProperties(resSaveVO, resInfoDO);
        resInfoDO.setModifier(contextHolder.getSessionContext().getUserId());
        resInfoDO.setCreator(contextHolder.getSessionContext().getUserId());
        resInfoDO.setIsDeleted(DeletedEnum.NO.getKey());
        resInfoDO.setGmtCreate(new Date());
        resInfoDO.setGmtModified(new Date());
        // 计算左右值
        resInfoDO.setResLft(resInfoBO.getResLft() + 2);
        resInfoDO.setResRgt(resInfoBO.getResRgt() + 2);
        resInfoDO.setResLevel(resInfoBO.getResLevel());
        return resInfoDO;
    }


    /**
     * 根据资源名称和系统类型查询资源集合
     *
     * @param resName
     * @param sysId
     * @return
     */
    private List<ResInfoBO> queryRessourceListByResNameAndSysId(String resName, Integer sysId) {
        // 根据左值排序查询资源
        List<ResInfoBO> resInfoBOList = resInfoMapper.queryResourceInfoList(resName, sysId);
        if (CollectionUtils.isEmpty(resInfoBOList)) {
            return Lists.newArrayList();
        }
        // 查询该节点的孩子节点
        for (ResInfoBO resInfoBO : resInfoBOList) {
            if (resInfoBO == null) {
                continue;
            }
            // 取到右值
            Integer rgt = resInfoBO.getResRgt();
            // 取到左值
            Integer lft = resInfoBO.getResLft();
            if (rgt == null || lft == null) {
                continue;
            }
            // 如果右值 - 左值 = 1 说明没有孩子节点
            if (rgt - lft == BaseBizEnum.FIRST.getKey()) {
                continue;
            }
            // 如果有孩子节点，则查询孩子节点
            Integer id = resInfoBO.getId();
            if (id == null) {
                continue;
            }
            List<ResInfoBO> resChildList = resInfoMapper.queryResourceInfoListByFatherId(id, sysId);
            if (CollectionUtils.isEmpty(resChildList)) {
                continue;
            }
            resInfoBO.setChildResList(resChildList);
        }
        return resInfoBOList;
    }


    /**
     * 根据系统类型查询资源集合
     *
     * @param sysId
     * @return
     */
    private List<ResInfoBO> queryRessourceListBySysId(Integer sysId) {
        // 根据左值排序查询资源
        List<ResInfoBO> resInfoBOList = resInfoMapper.queryResourceInfoList(null, sysId);
        return Arrays.asList(this.makeRessourceListToRootNode(resInfoBOList));
    }


    /**
     * 将资源整合成一颗树，最终返回根节点
     *
     * @param resInfoBOList 资源集合
     * @return
     */
    private ResInfoBO makeRessourceListToRootNode(List<ResInfoBO> resInfoBOList) {
        if (CollectionUtils.isEmpty(resInfoBOList)) {
            return null;
        }
        // 创建一颗数用于存放所有的父节点对应的孩子节点。key为fatherId，value为该父节点id对应的孩子节点
        Map<Integer, List<ResInfoBO>> map = CollectionUtil.listToMapGroup(resInfoBOList, new Function<ResInfoBO, Integer>() {
            @Override
            public Integer apply(ResInfoBO resInfoBO) {
                return resInfoBO.getFatherId();
            }
        });
        // 创建一个对象用于存放根节点
        ResInfoBO resInfo = null;
        // 循环遍历查询得到的List，将相同的fatherId设置到对应的父节点中
        for (ResInfoBO resInfoBO : resInfoBOList) {
            if (resInfoBO == null) {
                continue;
            }
            // 取出根节点的对象，进行引用传递
            if (resInfoBO.getFatherId() == 0) {
                resInfo = resInfoBO;
            }
            Integer id = resInfoBO.getId();
            if (id != null && map.containsKey(id)) {
                resInfoBO.setChildResList(map.get(id));
            }
        }
        return resInfo;
    }


    /**
     * 保存的资源参数校验
     *
     * @param resSaveVO
     */
    private void checkResSaveVO(ResSaveVO resSaveVO) {
        if (resSaveVO == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源信息不能为空");
        }
        if (StringUtil.isBlank(resSaveVO.getResSn())) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源编码不能为空");
        }
        if (StringUtil.isBlank(resSaveVO.getResName())) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源名称不能为空");
        }
        if (null == resSaveVO.getResType()) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源类型不能为空");
        }
        if (null == resSaveVO.getFatherId()) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源父id不能为空");
        }
        if (StringUtil.isBlank(resSaveVO.getResValue())) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源值不能为空");
        }
        if (null == resSaveVO.getSysId()) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "系统id不能为空");
        }
    }

    /**
     * 参数校验
     *
     * @param resUpdateVO
     */
    private void checkResUpdateVO(ResUpdateVO resUpdateVO) {
        if (resUpdateVO == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源信息不能为空");
        }
        if (resUpdateVO.getId() == null) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "资源id不能为空");
        }
        if (null == resUpdateVO.getSysId()) {
            throw new BusinessCheckFailException(BaseErrorEnum.BNS_CHK_ERROR.getCode(), "系统id不能为空");
        }
    }
}
